<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Technology extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('admin/technology_model','technology_model');
	}
	
	public function index()
	{
		$this->lists();
	}
	
	public function lists($p=1)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = 'current';
		$menu['menu_4'] = '';
		$menu['menu_5'] = '';
		
		$data['lists'] = $this->technology_model->lists(1,500);
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('content','technology_view',$data);
		$this->template->render();
	}
	
	public function add()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = 'current';
		$menu['menu_4'] = '';
		$menu['menu_5'] = '';
		
		$data['message'] = $this->session->flashdata('message');
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('content','technology_add_view',$data);
		$this->template->render();
	}
	
	public function add_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin");
			exit();
		}
		
		if(isset($_POST['title']) AND $_POST['title']!='' AND isset($_POST['detail']) AND $_POST['detail']!=''  AND isset($_POST['date']) AND $_POST['date']!=''){
			$input['title'] = $_POST['title'];
			$input['detail'] = $_POST['detail'];
			$input['date'] = strtotime($_POST['date']);
			$id = $this->technology_model->add($input);
			
			if($id!=0){
				if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
					$this->load->library('ciupload');
					$this->ciupload->Upload($_FILES['img']);
					if ($this->ciupload->uploaded){
						$this->ciupload->image_resize = true;
						$this->ciupload->image_ratio_fill = true;
						$this->ciupload->image_x = 156;
						$this->ciupload->image_y = 100;
						$this->ciupload->Process(APPPATH.'../assets/image/technology/'.$id);
						
						if ($this->ciupload->processed){
							$input['id'] = $id;
							$input['img'] = $this->ciupload->file_dst_name;
							$result = $this->technology_model->update_img($input);
							if($result==0){
								@unlink(APPPATH.'../assets/image/technology/'.$id.'/'.$this->ciupload->file_dst_name);
								$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
							}else{
								redirect(base_url()."admin/technology");
							}
						}else{
							$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
							redirect(base_url()."admin/technology");
						}
					}else{
						$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
						redirect(base_url()."admin/technology");
					}
				}else{
					$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
					redirect(base_url()."admin/technology");
				}
			}else{
				$this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
				redirect(base_url()."admin/technology/add");
			}
		}else{
			$this->session->set_flashdata('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
			redirect(base_url()."admin/technology/add");
		}
	}
	
	public function edit($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		$menu['menu_1'] = '';
		$menu['menu_2'] = '';
		$menu['menu_3'] = 'current';
		$menu['menu_4'] = '';
		$menu['menu_5'] = '';
		
		$data['info'] = $this->technology_model->get_info($id);
		$data['message'] = $this->session->flashdata('message');
		
		$this->template->set_template('admin');
		$this->template->write_view('menu','menu_view',$menu);
		$this->template->write_view('content','technology_edit_view',$data);
		$this->template->render();
	}
	
	public function edit_process()
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(isset($_POST['id']) AND isset($_POST['title']) AND $_POST['title']!='' AND isset($_POST['detail']) AND $_POST['detail']!=''  AND isset($_POST['date']) AND $_POST['date']!=''){
			$id = $_POST['id'];
			$input['id'] = $id;
			$input['title'] = $_POST['title'];
			$input['detail'] = $_POST['detail'];
			$input['date'] = strtotime($_POST['date']);
			$check = $this->technology_model->update($input);
			
			if($check){
				if(isset($_FILES["img"]) && is_uploaded_file($_FILES["img"]["tmp_name"]) && $_FILES["img"]["tmp_name"]!=''){
					$this->load->library('ciupload');
					$this->ciupload->Upload($_FILES['img']);
					if ($this->ciupload->uploaded){
						$this->ciupload->image_resize = true;
						$this->ciupload->image_ratio_fill = true;
						$this->ciupload->image_x = 156;
						$this->ciupload->image_y = 100;
						$this->ciupload->Process(APPPATH.'../assets/image/technology/'.$id);
						
						if ($this->ciupload->processed){
							$input['id'] = $id;
							$input['img'] = $this->ciupload->file_dst_name;
							$old_img = $this->technology_model->get_img($input);
							$result = $this->technology_model->update_img($input);
							if($result==0){
								@unlink(APPPATH.'../assets/image/technology/'.$id.'/'.$this->ciupload->file_dst_name);
								$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');;
				redirect(base_url()."admin/technology/edit/{$id}");
							}else{
								if($old_img->img!='' AND file_exists(APPPATH.'../assets/image/technology/'.$id.'/'.$old_img->img)){
									@unlink(APPPATH.'../assets/image/technology/'.$id.'/'.$old_img->img);
								}
								redirect(base_url()."admin/technology");
							}
						}else{
							$this->session->set_flashdata('message', 'ไม่สามารถบันทึกรูปภาพได้ กรุณาแก้ไข');
							redirect(base_url()."admin/technology/edit/{$id}");
						}
					}
				}else{
					redirect(base_url()."admin/technology");
				}
			}else{
				$this->session->set_flashdata('message', 'ไม่สามารถบันทึกข้อมูลได้ กรุณาลองใหม่อีกครั้ง');
				redirect(base_url()."admin/technology/edit/{$id}");
			}
		}else{
			
			$this->session->set_flashdata('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
			redirect(base_url()."admin/technology/edit/{$_POST['id']}");
		}
	}
	
	public function del($id)
	{
		if($this->session->userdata('admin_logged_in') == FALSE){
			redirect(base_url()."admin/login");
			exit();
		}
		
		if(($id+0)>0){
			if($this->technology_model->delete($id+0)){
				$this->load->helper('devlism');
				devlism_remove_dir(APPPATH.'../assets/image/technology/'.$id);
			}
			redirect(base_url()."admin/technology");
		}else{
			redirect(base_url()."admin/technology");
		}
	}
}

/* End of file technology.php */
/* Location: ./application/controllers/technology.php */